package com.dreamwhale.machinery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dreamwhale.machinery.adviceException.DragonException;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.dao.VipInfoMapper;
import com.dreamwhale.machinery.entity.VipInfo;
import com.dreamwhale.machinery.entity.VipUserInfo;
import com.dreamwhale.machinery.dao.VipUserInfoMapper;
import com.dreamwhale.machinery.pay.ali.server.AliPayService;
import com.dreamwhale.machinery.pay.weChat.server.WeChatPayServer;
import com.dreamwhale.machinery.service.VipUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-07-08
 */
@Service
public class VipUserInfoServiceImpl extends ServiceImpl<VipUserInfoMapper, VipUserInfo> implements VipUserInfoService {


    @Resource
    private AliPayService aliPayService;
    @Resource
    private WeChatPayServer weChatPayServer;
    @Resource
    private VipUserInfoMapper vipUserInfoMapper;
    @Resource
    private VipInfoMapper vipInfoMapper;

    @Override
    public DataResult buyVip(Integer vipId, Integer payType, Integer userId) {
        VipInfo vipInfo = vipInfoMapper.selectById(vipId);
        if (vipInfo == null) throw new DragonException("会员不存在");
        BigDecimal vipPrice = vipInfo.getVipPrice();
        BigDecimal discount = BigDecimal.ZERO;
        //优惠券计算
//        if(!CollectionUtils.isEmpty(dcId)){
//            List<PushUserCoupon> pushUserCouponList = pushUserCouponMapper.selectList(new QueryWrapper<PushUserCoupon>().in("discount_coupon_id", dcId).eq("discount_coupon_status",0));
//            boolean match = pushUserCouponList.stream().allMatch(v -> v.getDiscountCouponStatus().equals(0) && v.getUid().equals(userId));
//            if (!match) throw new DragonException("优惠券选择错误");
//            Map<Integer, Long> groupingByCouponList = pushUserCouponList.stream().collect(Collectors.groupingBy(PushUserCoupon::getCid, Collectors.counting()));
//            List<PushDiscountCoupon> pushDiscountCoupons = pushDiscountCouponMapper.selectList(new QueryWrapper<PushDiscountCoupon>().in("discount_coupon_id", groupingByCouponList.keySet()));
//            discount = pushDiscountCoupons.stream().map(v -> v.getDiscountCouponValue().multiply(BigDecimal.valueOf(groupingByCouponList.get(v.getDiscountCouponId())))).reduce(BigDecimal.ZERO, BigDecimal::add);
//        }
        //String orderId = pushOrderService.createOrder(userId, dcId, "会员充值", discount, payment);
        BigDecimal payment = vipPrice.subtract(discount);


        VipUserInfo vipUserInfo1 = vipUserInfoMapper.inquiryVipExpirationTime(userId);
        LocalDateTime expirationTime = vipUserInfo1 == null ? LocalDateTime.now() : vipUserInfo1.getExpirationTime();
        VipUserInfo vipUserInfo = new VipUserInfo();
        vipUserInfo.setVid(vipId);
        vipUserInfo.setUid(userId);
        vipUserInfo.setCreateTime(LocalDateTime.now());
        vipUserInfo.setPayOrderId(IdWorker.getIdStr());
        vipUserInfo.setPayMoney(payment);
        vipUserInfo.setExpirationTime(expirationTime.compareTo(LocalDateTime.now()) > 0
                ? expirationTime.plusMonths(vipInfo.getVipMonth())
                : LocalDateTime.now().plusMonths(vipInfo.getVipMonth())
        );
        if (vipUserInfoMapper.insert(vipUserInfo) == 1) {
            switch (payType) {
                case 0:
                    throw new DragonException("当前支付未开通");
                case 1:
                    return aliPayService.vipRecharge(vipUserInfo.getPayOrderId(), userId);
                case 2:
                    return weChatPayServer.vipRecharge(vipUserInfo.getPayOrderId(), userId);
                default:
                    throw new DragonException("未知支付方式");
            }
        }


        return DataResult.ok();
    }
}
